package com.ruoyi.project.system.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.SysGoods;
import com.ruoyi.project.system.service.ISysGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 商品列表Controller
 *
 * @author orange
 * @date 2022-04-09
 */
@RestController
@RequestMapping("/system/goods")
public class SysGoodsController extends BaseController {
  @Autowired private ISysGoodsService sysGoodsService;

  /** 查询商品列表列表 */
  @PreAuthorize("@ss.hasPermi('system:goods:list')")
  @GetMapping("/list")
  public TableDataInfo list(SysGoods sysGoods) {
    startPage();
    List<SysGoods> list = sysGoodsService.selectSysGoodsList(sysGoods);
    return getDataTable(list);
  }

  /** 导出商品列表列表 */
  @PreAuthorize("@ss.hasPermi('system:goods:export')")
  @Log(title = "商品列表", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  public void export(HttpServletResponse response, SysGoods sysGoods) {
    List<SysGoods> list = sysGoodsService.selectSysGoodsList(sysGoods);
    ExcelUtil<SysGoods> util = new ExcelUtil<SysGoods>(SysGoods.class);
    util.exportExcel(response, list, "商品列表数据");
  }

  /** 获取商品列表详细信息 */
  @PreAuthorize("@ss.hasPermi('system:goods:query')")
  @GetMapping(value = "/{goodsId}")
  public AjaxResult getInfo(@PathVariable("goodsId") Long goodsId) {
    return AjaxResult.success(sysGoodsService.selectSysGoodsByGoodsId(goodsId));
  }

  /** 新增商品列表 */
  @PreAuthorize("@ss.hasPermi('system:goods:add')")
  @Log(title = "商品列表", businessType = BusinessType.INSERT)
  @PostMapping
  public AjaxResult add(@RequestBody SysGoods sysGoods) {
    return toAjax(sysGoodsService.insertSysGoods(sysGoods));
  }

  /** 修改商品列表 */
  @PreAuthorize("@ss.hasPermi('system:goods:edit')")
  @Log(title = "商品列表", businessType = BusinessType.UPDATE)
  @PutMapping
  public AjaxResult edit(@RequestBody SysGoods sysGoods) {
    return toAjax(sysGoodsService.updateSysGoods(sysGoods));
  }

  /** 删除商品列表 */
  @PreAuthorize("@ss.hasPermi('system:goods:remove')")
  @Log(title = "商品列表", businessType = BusinessType.DELETE)
  @DeleteMapping("/{goodsIds}")
  public AjaxResult remove(@PathVariable Long[] goodsIds) {
    return toAjax(sysGoodsService.deleteSysGoodsByGoodsIds(goodsIds));
  }
}
